package com.base.mail.presenter;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;

import com.base.commonlib.base.presenter.BasePresenter;
import com.base.commonlib.bean.User;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.http.utils.CallBackImpl;
import com.base.mail.bean.ContactEntity;
import com.base.mail.view.ConstractView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import io.reactivex.disposables.Disposable;


/**
 * Created by weikailiang on 2019/9/19.
 */

public class ConstratPresenter extends BasePresenter<ConstractView> {

    public ArrayList<ContactEntity> mData = new ArrayList<>();
    private ArrayList<ContactEntity> mInitialData = new ArrayList<>();
    private ArrayList<ContactEntity> mSearchData = new ArrayList<>();
    public ConstratPresenter(ConstractView view, Activity activity) {
        super(view, activity);
    }

    @Override
    public void start() {

    }
    public void getContacts(){
        mView.showPb();
        HashMap<String, String> param = new HashMap<>();
        Disposable enqueue = RealNetWorkUtil.postContent().method("organization-service-compose/user/getUsersByDept").content(param)
                .build().enqueueForArray(User.class, new CallBackImpl<List<User>>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb();
                        mView.showMes(msg);
                        mView.showReload();
                    }

                    @Override
                    public void onNext(List<User> users) {
                        mView.hidePb();
                        mInitialData.clear();
                        mData.clear();
                        for (User user : users) {
                            mInitialData.add(new ContactEntity(user.getUserId(), user.getUserName()));
                        }
                        Collections.sort(mInitialData);

                        // TODO: 2019/3/14 low
                        new Handler(Looper.getMainLooper()).post(new Runnable() {
                            @Override
                            public void run() {
                                mData.addAll(mInitialData);
                                mView.setData(mData);
                                mView.hideReload();
                            }
                        });


                    }
                });

        mCompositeDisposable.add(enqueue);

    }
    public void doSearch(String keyWords){
        mSearchData.clear();
        if (TextUtils.isEmpty(keyWords)) {
            mSearchData.addAll(mInitialData);
        } else {
            String str = null;
            for (ContactEntity item : mInitialData) {
                str = item.getRealname() + item.getNameSpell();
                if (str != null && str.contains(keyWords)) {
                    mSearchData.add(item);
                }
            }
        }
        mView.showSearch(mSearchData);
    }
}
